package com.winsoar.cams.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winsoar.cams.dao.PersonMapper;
import com.winsoar.cams.entity.Foodcard;
import com.winsoar.cams.pojo.Person;
import com.winsoar.cams.service.FoodcardService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Foodcard)表控制层
 *
 * @author makejava
 * @since 2021-02-22 16:46:41
 */
@Controller
@RequestMapping("foodcard")
public class FoodcardController {
    /**
     * 服务对象
     */
    @Resource
    private FoodcardService foodcardService;
   @Resource
    private PersonMapper personMapper;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Foodcard selectOne(Long id) {

        return this.foodcardService.queryById(id);
    }

    @RequestMapping("/toFoodCard")
    public String toFoodCard() {
        return "h-ui/FoodCard/index";

    }

    @RequestMapping("/toAddCard")
    public String toAddCard(Model model) {
        List<Person> teacherList = personMapper.selectCardPerson();
        model.addAttribute("teacherList",teacherList);
        return "h-ui/FoodCard/clardadd";
    }

    /*    获取饭卡信息*/
    @RequestMapping("/getInfo")
    @ResponseBody
    public JSONArray getInfo() {
        List<Map<String, Object>> cardInfo = foodcardService.getInfo();
        JSONArray jsonObject = (JSONArray) JSON.toJSON(cardInfo);
        return jsonObject;
    }

    /*    获取饭卡信息*/
    @RequestMapping("/mgetStuInfo")
    @ResponseBody
    public JSONObject mgetStuInfo(String Pid) {
        Map<String, Object> StuInfo = foodcardService.getStuInfo(Pid);
        JSONObject object = new JSONObject(StuInfo);
        return object;
    }  /*    学生饭卡充值*/

    @RequestMapping("/mgetStuCz")
    @ResponseBody
    public JSONObject mupdateStuCz(String jinE, String Pid) {
        HashMap<String, Object> map = new HashMap<>();
        int StuInfo = foodcardService.mupdateStuCz(jinE, Pid);
        if (StuInfo > 0) {
            map.put("code", "200");//充值成功
        } else {
            map.put("code", "400");//充值失败
        }
        JSONObject object = new JSONObject(map);
        return object;
    }

    /*    获取饭卡信息*/
    @RequestMapping("/mgetFKInfo")
    @ResponseBody
    public JSONArray mgetInfo() {
        List<Map<String, Object>> cardInfo = foodcardService.getInfo();
        JSONArray jsonObject = (JSONArray) JSON.toJSON(cardInfo);
        return jsonObject;
    }

    /*    停用or启用饭卡信息*/
    @RequestMapping("/msetztai")
    @ResponseBody
    public HashMap<String, Object> msetztai(@RequestParam String zt, String cardNum) {
        HashMap<String, Object> map = new HashMap<>();

        int setztai = foodcardService.setztai(zt, cardNum);
        if (setztai == 0) {
            map.put("code", 400);
        } else {
            map.put("code", 200);
        }
        return map;
    }

}
